package com.jf3q.admin.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author ASUS
 * @date 2024/4/13
 * @apiNote
 */
public class JwtUtil {

    public static String getToken(String username){
        return Jwts.builder().setSubject(username)
                .setHeaderParam("typ", "JWT")
                .setExpiration(new Date(System.currentTimeMillis() + 3600000))
                .signWith(SignatureAlgorithm.HS256, "lxl-jwt").compact();
    }

    public static String getUsername(String token){
        try {
            return Jwts.parser().setSigningKey("lxl-jwt").parseClaimsJws(token).getBody().getSubject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
